<template>
  <tr>
    <td><input type="checkbox" :checked="item.checked" @change="changeCheck" /> <span>全选</span></td>
    <td>{{ item.name }}</td>
    <td>{{ item.price }}</td>
    <td>
      <button @click="onBtnClick(-1)">-</button>{{ item.num}}<button @click="onBtnClick(1)">+</button>
    </td>
    <td>{{ item.price * item.num }}</td>
    <td>
      <button @click="del(item.name)">删除</button>
    </td>
  </tr>
</template>

<script>
import { mapMutations } from 'vuex'
export default {
  name: 'TablePage',
  props: {
    item: Object
  },
  methods: {
    // 加减
    onBtnClick (num) {
      const newCount = this.item.num + num
      if (newCount < 0) return
      this.$store.commit('cart/onBtnClick', {
        name: this.item.name,
        num: newCount
      })
    },
    // 删除
    ...mapMutations('cart', ['del']),
    // 全选状态改变
    changeCheck (e) {
      this.$store.commit('cart/changeCheck', {
        name: this.item.name,
        checked: e.target.checked
      })
    }
  }

}
</script>

<style></style>